package dictservicelogic

import (
	"context"
	"encoding/json"

	"github.com/blog-zero/rpc/sys/internal/svc"
	"github.com/blog-zero/rpc/sys/sysclient"

	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type DictChildenListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDictChildenListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DictChildenListLogic {
	return &DictChildenListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DictChildenListLogic) DictChildenList(in *sysclient.DictChildenListReq) (*sysclient.DictChildenListResp, error) {
	all, err := l.svcCtx.DictChildenModel.FindAll(l.ctx, in)
	count, _ := l.svcCtx.DictChildenModel.Count(l.ctx, in)

	if err != nil {
		reqStr, _ := json.Marshal(in)
		logx.WithContext(l.ctx).Errorf("查询子字典列表信息失败,参数:%s,异常:%s", reqStr, err.Error())
		return nil, err
	}
	var list []*sysclient.DictChildenListData
	for _, dictChilden := range *all {
		list = append(list, &sysclient.DictChildenListData{
			Id:             dictChilden.Id,
			ParentId:       dictChilden.ParentId,
			Label:          dictChilden.Label,
			Value:          dictChilden.Value,
			Description:    dictChilden.Description,
			Sort:           int64(dictChilden.Sort),
			Remarks:        dictChilden.Remarks.String,
			CreateBy:       dictChilden.CreateBy,
			CreateTime:     dictChilden.CreateTime.Format("2006-01-02 15:04:05"),
			LastUpdateBy:   dictChilden.UpdateBy.String,
			LastUpdateTime: dictChilden.UpdateTime.Format("2006-01-02 15:04:05"),
			Status:         dictChilden.Status,
		})
	}

	logc.Infof(l.ctx, "查询子字典列表信息,参数：%+v,响应：%+v", in, list)
	return &sysclient.DictChildenListResp{
		Total: count,
		List:  list,
	}, nil
}
